package com.bonc.rms.service.impl.tms;


import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.jpa.tms.DepartmentRepository;
import com.bonc.rms.jpa.tms.TalentRepository;
import com.bonc.rms.mapper.XDepartmentInfoMapper;
import com.bonc.rms.service.tms.IEmployeeService;

import com.bonc.rms.utils.impl.WastageInfoUtilImpl;

@Service
public class EmployeeServiceimpl implements IEmployeeService {
	@Autowired
	private TalentRepository talentRepository;

	@Autowired
	private DepartmentRepository departmentRepository;

	@Autowired
	XDepartmentInfoMapper xDepartmentInfoMapper;

	@Autowired
	WastageInfoUtilImpl wastageInfoUtilImpl;

	protected EntityManager entityManager = null;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	/**
	 * 工作年限分布
	 * 
	 * @param departmentId
	 * @return
	 */
	public Map<String, String> getWorkAgeDistribute(Integer departmentStandardId) {
		LogManager.Method("Int Service : EmployeeServiceimpl -> getWorkAgeDistribute");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateSt = sdf.format(date);

		Map<String, String> workAgeMap = new HashMap<>();
		List<Integer> departmentIdlist = new ArrayList<>();
		departmentIdlist = wastageInfoUtilImpl
				.getDepartmentId(departmentStandardId);
		if ((departmentIdlist == null || departmentIdlist.size() == 0)
				&& departmentStandardId > 2) {

			workAgeMap.put("一年以内", 0 + "");

			workAgeMap.put("一到三年", 0 + "");

			workAgeMap.put("三到五年", 0 + "");

			workAgeMap.put("五到十年", 0 + "");

			workAgeMap.put("十年以上", 0 + "");
		} else {

			int age1 = xDepartmentInfoMapper.getAllWorkAgeByDate(dateSt, 0, 1,
					departmentIdlist, departmentIdlist.size());
			workAgeMap.put("一年以内", age1 + "");

			int age2 = xDepartmentInfoMapper.getAllWorkAgeByDate(dateSt, 1, 3,
					departmentIdlist, departmentIdlist.size());
			workAgeMap.put("一到三年", age2 + "");

			int age3 = xDepartmentInfoMapper.getAllWorkAgeByDate(dateSt, 3, 5,
					departmentIdlist, departmentIdlist.size());
			workAgeMap.put("三到五年", age3 + "");

			int age4 = xDepartmentInfoMapper.getAllWorkAgeByDate(dateSt, 5, 10,
					departmentIdlist, departmentIdlist.size());

			workAgeMap.put("五到十年", age4 + "");
			int age5 = xDepartmentInfoMapper.getAllWorkAgeByDate(dateSt, 10,
					200, departmentIdlist, departmentIdlist.size());

			workAgeMap.put("十年以上", age5 + "");

		}
		LogManager.Method("Out Service : EmployeeServiceimpl -> getWorkAgeDistribute");
		return workAgeMap;
	}

	/**
	 * 获取部门名称
	 * 
	 * @param departmentId
	 * @return
	 */
//	public String getNameById(Integer departmentId) {
//		String str;
//		if (departmentId == 1) {
//			str = "BONC";
//		} else if (departmentId == 2) {
//			str = "总经办";
//		} else {
//			str = xDepartmentInfoMapper.findNameById(departmentId);
//		}
//
//		return str;
//	}

	/**
	 * 员工年龄分布
	 */
	public Map<String, String> getAgeDistribute(Integer departmentStandardId) {
		LogManager.Method("Int Service : EmployeeServiceimpl -> getAgeDistribute");
		Map<String, String> ageMap = new HashMap<>();
		List<Integer> departmentIdlist = new ArrayList<>();
		departmentIdlist = wastageInfoUtilImpl
				.getDepartmentId(departmentStandardId);
		if ((departmentIdlist == null || departmentIdlist.size() == 0)
				&& departmentStandardId > 2) {
			ageMap.put("50后", 0 + "");

			ageMap.put("60后", 0 + "");

			ageMap.put("70后", 0 + "");

			ageMap.put("80后", 0 + "");

			ageMap.put("90后", 0 + "");

		} else {

			int age1 = xDepartmentInfoMapper.getAllAge(5, departmentIdlist,
					departmentIdlist.size());
			ageMap.put("50后", age1 + "");

			int age2 = xDepartmentInfoMapper.getAllAge(6, departmentIdlist,
					departmentIdlist.size());
			ageMap.put("60后", age2 + "");

			int age3 = xDepartmentInfoMapper.getAllAge(7, departmentIdlist,
					departmentIdlist.size());
			ageMap.put("70后", age3 + "");

			int age4 = xDepartmentInfoMapper.getAllAge(8, departmentIdlist,
					departmentIdlist.size());
			ageMap.put("80后", age4 + "");

			int age5 = xDepartmentInfoMapper.getAllAge(9, departmentIdlist,
					departmentIdlist.size());
			ageMap.put("90后", age5 + "");

		}
		LogManager.Method("Out Service : EmployeeServiceimpl -> getAgeDistribute");
		return ageMap;
	}

}
